//
//  DrawingPage.m
//  DemoDrawingShapesInCanvasUsingObjects
//
//  Created by duc nguyen van on 8/14/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "DrawingPage.h"


@implementation DrawingPage
@synthesize type;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		type = handle_shape;
		arrViewShapes = [[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark Process

-(void)removeSelecteView{
	vSelected.layer.borderColor = [[UIColor whiteColor] CGColor];
	vSelected.layer.borderWidth = 0;
	//vSelected.layer.shadowRadius = 3;
	//vSelected.layer.shadowOpacity = 0.5;
}

- (void)setSelectedView{
	vSelected.layer.borderColor = [[UIColor blueColor] CGColor];
	vSelected.layer.borderWidth = 5;
	//vSelected.layer.shadowRadius = 3;
	//vSelected.layer.shadowOpacity = 0.5;
}


#pragma mark -
#pragma mark delegateShape

-(void)actionTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event andView:(UIView *)v andVexter:(CGPoint)vex{
	if (type == handle_shape) {
		
		CGPoint local = vSelected.center;
		local.x += vex.x;
		local.y += vex.y;
		vSelected.center = local;
	}
}

-(void)actionTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event andView:(UIView *)v{
	if (type == handle_shape) {
		[self removeSelecteView];
		vSelected = v;
		[self setSelectedView];
	}
	else {
		UITouch *touch = [touches anyObject];
		CGPoint pBegin =[touch locationInView:self];
		
		BPoint = pBegin;
	}

}

#pragma mark -
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
	CGContextRef contextRef = UIGraphicsGetCurrentContext();
	CGContextSetRGBStrokeColor(contextRef, 1.0, 0, 0, 1);
	//CGContextSetLineWidth(contextRef, 5.0);
	
	switch (type) {
		case Circle_shape:
			CGContextAddEllipseInRect(contextRef, CGRectMake(BPoint.x, BPoint.y, MPoint.x - BPoint.x, MPoint.y - BPoint.y));
			break;
			
		case Rectangle_shape:
			CGContextAddRect(contextRef, CGRectMake(BPoint.x, BPoint.y, MPoint.x - BPoint.x, MPoint.y - BPoint.y));
			break;

		case Line_shape:
			CGContextMoveToPoint(contextRef, BPoint.x, BPoint.y);
			CGContextAddLineToPoint(contextRef, MPoint.x, MPoint.y);
			break;
			
		default:
			//clear color 
			CGContextSetFillColorWithColor(contextRef, [UIColor  whiteColor].CGColor);
			break;
	}
	CGContextStrokePath(contextRef);
	CGContextFillPath(contextRef);
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	BPoint = [touch locationInView:self];
	
	NSLog(@"B x:%f y:%f",BPoint.x, BPoint.y);
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	MPoint = [touch locationInView:self];
	
	switch (type) {
		case Line_shape:
			[self setNeedsDisplay];
			break;
			
		case Circle_shape:
			[self setNeedsDisplay];
			break;

		case Rectangle_shape:
			[self setNeedsDisplay];
			break;

		default:
			[self setNeedsDisplay];
			break;
	}
}

-(void)createNewRecWithBPoint:(CGPoint)bp andEPoint:(CGPoint)ep{
	Rectangle *rec = [[Rectangle alloc] initWithFrame:CGRectMake(bp.x, bp.y, ep.x - bp.x, ep.y - bp.y)];
	rec.backgroundColor = [UIColor clearColor];
	rec.delegate = self;
	
	[self addSubview:rec];
	[rec release];
}

-(void)createNewCirWithBPoint:(CGPoint)bp andEPoint:(CGPoint)ep{
	Circle *cir = [[Circle alloc] initWithFrame:CGRectMake(bp.x, bp.y, ep.x - bp.x, ep.y - bp.y)];
	cir.backgroundColor = [UIColor clearColor];
	cir.delegate = self;
	
	[self addSubview:cir];
	[cir release];
}

-(void)createNewLineWithBPoint:(CGPoint)bp andEPoint:(CGPoint)ep{
	float width_view = sqrt(pow((EPoint.x - BPoint.x),2) + pow((EPoint.y - BPoint.y),2));
	float rotationValue = atan((EPoint.y - BPoint.y) / (EPoint.x - BPoint.x));
	
	Line *lineView = [[Line alloc] initWithFrame:CGRectMake(BPoint.x, BPoint.y - 10, width_view,20)];
	lineView.backgroundColor = [UIColor clearColor];
	lineView.layer.anchorPoint = CGPointMake(0.0,0.5);
	
	CGPoint temp = lineView.center;
	temp.x -= lineView.frame.size.width / 2;
	lineView.center = temp;
	lineView.transform = CGAffineTransformMakeRotation(rotationValue);
	lineView.delegate = self;
	
	[self addSubview:lineView];
	[lineView release];
}

-(void)checkBPointAndEPoint{
	BOOL Bone = BPoint.x > EPoint.x && BPoint.y > EPoint.y;
	BOOL Btwo = BPoint.x > EPoint.x && BPoint.y < EPoint.y;
	
	if (Bone || Btwo) {
		CGPoint temp = BPoint;
		BPoint = EPoint;
		EPoint = temp;
	}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	//NSLog(@"count is %d", [arrViewShapes count]);
	
	UITouch *touch = [touches anyObject];
	EPoint = [touch locationInView:self];
	
	NSLog(@"Before: B x:%f y:%f and E x:%f and y:%f",BPoint.x, BPoint.y, EPoint.x, EPoint.y);
	[self checkBPointAndEPoint];
	NSLog(@"After: B x:%f y:%f and E x:%f and y:%f",BPoint.x, BPoint.y, EPoint.x, EPoint.y);
	
	switch (type) {
		case Line_shape:
			[self createNewLineWithBPoint:BPoint andEPoint:EPoint];
			break;
		case Rectangle_shape:
			[self createNewRecWithBPoint:BPoint andEPoint:EPoint];
			break;
		case Circle_shape:
			[self createNewCirWithBPoint:BPoint andEPoint:EPoint];
			break;

		default:
			break;
	}
}

- (void)dealloc {
    [super dealloc];
}


@end
